package com.gdk.ksoft.assembly.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gdk.ksoft.R;
import com.gdk.ksoft.data.common.SystemData;
import com.gdk.ksoft.data.entry.tables.ShoppingCartGoodsTable;
import com.gdk.ksoft.assembly.utils.extensions.ControlUtils;
import com.gdk.ksoft.assembly.utils.extensions.FormatUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * Created by Mrper on 2014/12/27.
 * 买赠本品购买数量编辑对话框
 */
public class GiveSelfChangeNumberDialog extends Dialog {

    public interface OnValueChangedListener{
        public void onValueChanged(int pieceNum,int singleNum,float price);
    }

    @ViewInject(R.id.dialog_giveself_changenumber_etPieceNum)private EditText etPieceNum;//商品件数
    @ViewInject(R.id.dialog_giveself_changenumber_etSingleNum)private EditText etSingleNum;//商品散数
    @ViewInject(R.id.dialog_giveself_changenumber_txtGoodsName)private TextView txtGoodsName;//商品名称
    @ViewInject(R.id.dialog_giveself_changenumber_txtGoodsFormat)private TextView txtGoodsFormat;//规格
    @ViewInject(R.id.dialog_giveself_changenumber_etPrice)private EditText etPrice;//单价
    @ViewInject(R.id.llPrice_container)private LinearLayout llPriceContainer;//价格显示板块

    private OnValueChangedListener listener;
    private ShoppingCartGoodsTable data;
    private boolean isSelfGoodsDialog = false;

    /**
     * 构造函数
     * @param context
     * @param isSelfGoodsDialog true - 本品添加窗口  false - 赠品添加窗口
     * @param data
     * @param listener
     */
    public GiveSelfChangeNumberDialog(final Context context,final boolean isSelfGoodsDialog,final ShoppingCartGoodsTable data,OnValueChangedListener listener) {
        super(context, R.style.WindowDialogTheme);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View rootView = View.inflate(context, R.layout.dialog_giveself_changenumber, null);
        setContentView(rootView);
        ViewUtils.inject(this, rootView);
        this.data = data;
        this.listener = listener;
        this.isSelfGoodsDialog = isSelfGoodsDialog;
        isShowPrice();//是否显示价格
        txtGoodsName.setText(data.mc);
        txtGoodsFormat.setText(data.gg+data.hsdw);
        etPieceNum.setText(data.xsdwsl == 0?"":data.xsdwsl+"");
        etSingleNum.setText(data.xsdwss == 0?"":data.xsdwss+"");
        etSingleNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable s) {
                if(Integer.valueOf(ControlUtils.getNumberStringFromTextView(etSingleNum)) >= data.hsgx){
                    Toast.makeText(context,"散数数量超过规格："+data.gg,Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * 是否显示价格
     */
    private void isShowPrice(){
        if(!isSelfGoodsDialog){
            llPriceContainer.setVisibility(View.GONE);
        }else{
            etPrice.setText(FormatUtils.exactNumber(data.bzxsdwj/data.hsgx,"0.00"));
            etPrice.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    checkPrice();//检测价格数据
                }
            });
        }
    }

    /**
     * 检测价格数据
     * @return
     */
    public boolean checkPrice(){
        float price = Float.valueOf(ControlUtils.getNumberStringFromTextView(etPrice));
        float singlePrice = data.bzxsdwj/data.hsgx;//散数价格
        if(SystemData.SaleManPower != 0){//当价格操作权限不是0时，即随意修改
            float rangePrice = singlePrice * (float)SystemData.SaleManPower;
            if(Math.abs(price-singlePrice)>rangePrice){
                Toast.makeText(getContext(),"当前价格值无效，价格波动范围在"
                        + FormatUtils.exactNumber(singlePrice-rangePrice,"0.00")
                        +"~"+FormatUtils.exactNumber(singlePrice+rangePrice,"0.00")
                        +"之间!",Toast.LENGTH_SHORT).show();
                return false;
            }
        }
        return true;
    }

    @OnClick(value={R.id.dialog_giveself_changenumber_txtCancel,R.id.dialog_giveself_changenumber_txtOk})
    public void onViewClick(View v){
        switch(v.getId()){
            case R.id.dialog_giveself_changenumber_txtCancel:
                this.dismiss();
                break;
            case R.id.dialog_giveself_changenumber_txtOk:
                if(this.listener!=null){
                    int pieceNum = Integer.valueOf(ControlUtils.getNumberStringFromTextView(etPieceNum));
                    int singleNum = Integer.valueOf(ControlUtils.getNumberStringFromTextView(etSingleNum));
                    float price = Float.parseFloat(ControlUtils.getNumberStringFromTextView(etPrice));
                    if(isSelfGoodsDialog && !checkPrice()) return;//如果是本品弹出的敞口，则检测价格数据
                    if(pieceNum == 0 && singleNum == 0){
                        Toast.makeText(getContext(),"请输入正确的商品数量!",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if(singleNum >= this.data.hsgx){
                        Toast.makeText(getContext(),"散数数量超过规格："+data.gg,Toast.LENGTH_SHORT).show();
                        return;
                    }
                    this.listener.onValueChanged(pieceNum, singleNum,price);
                }
                this.dismiss();
                break;
        }
    }



}
